package com.cenntro.nearr.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cenntro.nearr.base.BaseController;
import com.cenntro.nearr.common.Result;
import com.cenntro.nearr.redis.IRedisService;
import com.cenntro.nearr.util.MessageDigestUtil;
import com.cenntro.nearr.vo.EmployeeVO;


/**
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/emp")
public class EmployeeController extends BaseController{

	
	@Resource
	private com.cenntro.nearr.service.EmployeeService employeeService;
	@Resource
	private IRedisService iredisService;
	
	@CrossOrigin
	@RequestMapping(value = "/login") 
	@ResponseBody
	public Result login(EmployeeVO emp){
		String password = MessageDigestUtil.MD5(emp.getPassword());
		emp.setPassword(password);
		boolean flag = employeeService.login(emp);
		if (flag){
			return Result.ok(emp);
		}
		return Result.error("telephone is unregistered");
	}
	
	@CrossOrigin
	@RequestMapping(value = "/loginout", method = RequestMethod.GET)
	@ResponseBody
	public Result loginout(String token){
		String tel = iredisService.get(token);
		if (StringUtils.isNotBlank(tel)){
			EmployeeVO emp = new EmployeeVO();
			emp.setToken("");
			emp.setTelephone(tel);
			employeeService.update(emp);
		}
		return Result.ok();
	}
}
